<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Blog/classes/PhpFox_ComponentAddBlog.class.php, $Author:  $ */



/** Component for adding blogs
 * @version $Id: PhpFox_ComponentAddBlog.class.php 133 2007-12-28 15:43:39Z phpfoxsv $
 * @package module.blog
 *
 */
class PhpFox_Mod_Blog_ComponentAddBlog extends BaseComponent
{
    function process()
    {
        $oBlogItem = $this->_oModule->getItem('Blog');
        /* @var $oBlogItem PhpFox_Mod_Blog_ItemBlog */
        $oSecurityService = &App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        $oAccountService = &App::getModuleService('Account', 'Account');
        /* @var $oAccountService PhpFox_Mod_Account_ServiceAccount */
        $oReq = &App::getRequest();
        $oTpl = $this->_oModule->getTemplate();
        App::loadClass('Validator');
        App::loadClass('Text');
        $oTxt = &new Text();
        $oSecurityService->checkPerm('blog', 'full');
        $oSecurityService->checkAccess('blogs');
        if ($iId = $oReq->getInt('added'))
        {
            $oBlogItem->load($iId);
            $aItem = $oBlogItem->aData;
            $oTpl->assignSrc('aItem', $aItem);
            $oTpl->assign(array(
            	'bAdded' => 1,
            ));
            return $oTpl->fetch('AddBlog.html');
        }
        $sUser = $oSecurityService->getCurrentUserLogin();
        $aFields = array
        (
        	'val[title]'   => array('title' => App::format('Mod_Blog.add_form_title'), 'def' => 'required'),
        );
        $oValidator = &new Validator($aFields, array());
        $aValues = array();
        if ($oReq->getAction() == 'add')
        {
            if (!$oValidator->isValid($oReq->getAll()))
            {
                $this->addErrors($oValidator->getErrors());
            }
            $aValues = $oReq->getArray('val');
            if (!$aValues['content'])
            {
                $this->addErrors(App::format('Mod_Blog.error.no_content'));
            }
            if (!count($this->getErrors()))
            {
                $aValues['title'] 	   = $oTxt->_splitString($aValues['title'], 30);
	    	    $oBlogItem->aData['title']     = $oTxt->preParseText($aValues['title']);
                $oBlogItem->aData['user_id']      = App::getUser('id');
                $oBlogItem->aData['text']      = $oTxt->preParseText($oTxt->parseText($aValues['content'],0,0,1));
                $oBlogItem->aData['time']      = time();
                $oBlogItem->aData['tags']      = strip_tags($oTxt->preParseText($oTxt->ShortenText($aValues['tags'], 75)));
                $iId = $oBlogItem->insert();
                $oAccountService->changeUserInfo($oSecurityService->getCurrentUser(), 'journal');
                App::gotoUrl('public.account.submit.add-blog', array('added' => $iId));
            }
        }
        $oTpl->assignSrc('sValMess', $oValidator->makeJsMess());
        $oTpl->assignSrc('sValJs', $oValidator->makeJS());
        $oTpl->assign(array(
        	'aValues' => $aValues,
        	'aErrors' => $this->getErrors(),
        ));
        return $oTpl->fetch('AddBlog.html');
    }
}
?>